﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using CSCV.Group8.BlogRollWebPart.Helper;

namespace CSCV.Group8.BlogRollWebPart.Model
{
    /// <summary>
    /// Represent the post of blog
    /// </summary>
    public class BRPost
    {
        #region prop
        /// <summary>
        /// The unique ID for the Post
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// The Post ID
        /// </summary>
        public string PostID { get; set; }

        /// <summary>
        /// The Post Title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// The Post Body
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// The Post Published Date
        /// </summary>
        public DateTime PublishedDate { get; set; }

        /// <summary>
        /// The Post Modified Date
        /// </summary>
        public DateTime DateModified { get; set; }

        /// <summary>
        /// The Author of Post
        /// </summary>
        public string Author { get; set; }

        /// <summary>
        /// The Avarta of Author
        /// </summary>
        public string PictureURL { get; set; }

        /// <summary>
        /// Is avatar can be show ?
        /// </summary>
        public bool ShowAvatar { get; set; }

        /// <summary>
        /// The Url of the post
        /// </summary>
        public string PostURL { get; set; }

        /// <summary>
        /// The number of comment of this post
        /// </summary>
        public int CommentsCount
        {
            get
            {
                if (Comments != null) return Comments.Count;
                return 0;
            }
        }

        /// <summary>
        /// The list of the comments of this post
        /// </summary>
        public List<BRComment> Comments { get; set; }

        /// <summary>
        /// The SPWeb of this post, which is the blog contain this post
        /// </summary>
        public string PostSPWebUrl { get; set; }
        #endregion

        #region ctor
        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="spli">SPListItem represent the blog post</param>
        /// <param name="web">SPWeb represent the blog contain this post</param>
        /// <param name="showavatar">Is avarta should be show ?</param>
        public BRPost(SPListItem spli, string  weburl, bool showavatar)
        {
            try
            {
                if (spli != null)
                {
                    this.ShowAvatar = showavatar;
                    this.Title = spli.GetFormattedValue(HelperConst.TITLE_TAG);
                    this.PostID = spli.GetFormattedValue(HelperConst.ID_TAG);
                    this.ID = spli.UniqueId.ToString();
                    this.Content = spli.GetFormattedValue(HelperConst.POST_CONTENT_TAG);
                    this.PublishedDate = (DateTime)spli[HelperConst.PUBLISHED_DATE_TAG];
                    this.DateModified = (DateTime)spli[HelperConst.DATE_MODIFIED_TAG];
                    this.PostSPWebUrl = weburl;
                    this.PostURL = HelperLib.GetPostPath(weburl, PostID);
                    this.Author = spli.GetFormattedValue(HelperConst.AUTHOR_TAG);
                    this.PictureURL = HelperLib.GetUserProfilePictureURL(HelperLib.GetSPUser(spli, HelperConst.AUTHOR_TAG).Name);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region method



        /// <summary>
        /// Set the list of comments to the post
        /// </summary>
        /// <param name="lstComment">List of comments to be set to the post</param>
        public void SetComment(List<BRComment> lstComment)
        {
            Comments = new List<BRComment>();
            try
            {
                Comments = lstComment;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        #endregion
    }
}
